/*
 * Copyright 2007 Robert Hanson <iamroberthanson AT gmail.com>
 * Copyright 2009 Matias Kruk <kruk.matias AT gmail.com>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *    http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.gwtsandbox.gears.orm.client;

import java.util.Date;
import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.RootPanel;


public class GearsORMMain implements EntryPoint
{
	public void onModuleLoad ()
    {		
		//Window.alert("Objeto");
        GearsDataStore ds2 = (GearsDataStore) GWT.create(Objeto.class);
        GearsDataStore ds = (GearsDataStore) GWT.create(Request.class);
        
        
        Objeto o = new Objeto();
        o.setRequestId(new Long(8000));
        o.setShortTitle("Really Short");
        o.setStatus("ASSIGNED");
        o.setLastUpdated(new Date());
        o.setTestData((byte) 169);
        o.setStarDate(13254.6879);
        
        Objeto o2 = new Objeto();
        o2.setRequestId(new Long(900));
        o2.setShortTitle("Really Short");
        o2.setStatus("ASSIGNED");
        o2.setLastUpdated(new Date());
        o2.setTestData((byte) 169);
        o2.setStarDate(13254.6879);
        
        Request r = new Request();
        r.setRequestId(new Long(300));
        r.setShortTitle("This is a short title");
        r.setStatus("ASSIGNED");
        r.setLastUpdated(new Date());
        r.setTestData((byte) 168);
        r.setStarDate(12345.6789);
        r.setIdObject(o.getRequestId());
        
        Request r2 = new Request();
        r2.setRequestId(new Long(800));
        r2.setShortTitle("Really Short");
        r2.setStatus("ASSIGNED");
        r2.setLastUpdated(new Date());
        r2.setTestData((byte) 169);
        r2.setStarDate(13254.6879);
        r2.setIdObject(o2.getRequestId());

        
        try {
        	ds.save(r);
            ds.save(r2);
            ds2.save(o);
            ds2.save(o2);
            Request rs = (Request) ds.findById(new Long(800));

            String log = "***** Find By ID *****\n";
            log += rs.getRequestId() + "\n";
            log += rs.getShortTitle() + "\n";
            log += rs.getStatus() + "\n";
            log += rs.getLastUpdated() + "\n";
            log += Byte.toString(rs.getTestData()) + "\n";
            log += Double.toString(rs.getStarDate());

            RootPanel.get().add(new HTML("<pre>" + log + "</pre>"));
            
            GearsQuery q = ds.getNamedQuery(Request.FIND_BY_SHORT_TITLE);
            q.setString(0, "Really Short");
            
            Request[] requests = (Request[]) q.execute();
            log = "***** Named Query - should return 1 record *****\n";
            log += "Record returned: " + requests.length + "\n";
            for (int i = 0; i < requests.length; i++) {
                rs = requests[i];
                if (i > 1) log += "\n";
                log += rs.getRequestId() + "\n";
                log += rs.getShortTitle() + "\n";
                log += rs.getStatus() + "\n";
                log += rs.getLastUpdated() + "\n";
                log += Byte.toString(rs.getTestData()) + "\n";
                log += Double.toString(rs.getStarDate()) + "\n";
            }

            RootPanel.get().add(new HTML("<pre>" + log + "</pre>"));
            
            if (requests.length > 0) {
                ds.delete(requests[0]);

                requests = (Request[]) q.execute();
                log = "***** Named Query Following Delete - should return 0 records *****\n";
                log += "Record returned: " + requests.length + "\n";
                for (int i = 0; i < requests.length; i++) {
                    rs = requests[i];
                    log += rs.getRequestId() + "\n";
                    log += rs.getShortTitle() + "\n";
                    log += rs.getStatus() + "\n";
                    log += rs.getLastUpdated() + "\n";
                    log += Byte.toString(rs.getTestData()) + "\n";
                    log += Double.toString(rs.getStarDate()) + "\n\n";
                }

                RootPanel.get().add(new HTML("<pre>" + log + "</pre>"));
            }
        }
        catch (Exception e) {
            Window.alert(e.getMessage());
        }
        
    }

}
